﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectSemIII.Entity;

namespace ProjectSemIII
{
    public class SurveyProcess
    {
        private project_sem3Entities projectEntity;
        private List<SurveyCatalog> listCatalog;
        public SurveyProcess()
        {
            projectEntity = new project_sem3Entities();
        }


        public Survey getSurveyDefault(int role)
        {
            Survey sv = new Survey();
            try
            {
                sv = (from s in projectEntity.Surveys
                      where s.SurveyPlayer == role
                      where s.SurveyStatus == 1
                      select s).ToList().Last();
            }
            catch
            {

            }
            return sv;
        }

        //Check survey name is ready exits function
        private bool checkCatalogNameAvailable(string _catalogName)
        {
            bool flag = false;
            string catalogName = _catalogName.ToLower();
            try
            {
                var catalog = (from c in projectEntity.SurveyCatalogs
                               where c.CatalogName.ToLower() == catalogName
                               select c).ToList();
                if (catalog.Count == 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        // Create new catalog survey function

        public int createNewCatalogSurvey(string _catalogName)
        {
            string catalogName = _catalogName;
            int result = 0;
            if (checkCatalogNameAvailable(catalogName))
            {
                try
                {
                    SurveyCatalog catalog = new SurveyCatalog();
                    catalog.CatalogName = catalogName;
                    projectEntity.SurveyCatalogs.Add(catalog);
                    projectEntity.SaveChanges();
                    result = 10;
                }
                catch
                {
                    result = 20;
                }
            }
            else
            {
                result = 30;
            }
            return result;
        }

        // Get all survey catalog function
        public List<SurveyCatalog> getAllSurveyCatalog()
        {
            try
            {
                listCatalog = (from c in projectEntity.SurveyCatalogs
                               select c).ToList();
            }
            catch
            {
                listCatalog = null;
            }
            return listCatalog;
        }

        // Update survey catalog by id survey catalog function
        public int updateSurveyCatalog(int _surveyCatalogID, string _surveyCatalogName)
        {
            int result = 0;
            int surveyCatalogID = _surveyCatalogID;
            string surveyCatalogName = _surveyCatalogName;
            if (checkCatalogNameAvailable(surveyCatalogName))
            {
                try
                {
                    var surveyCatalog = (from sc in projectEntity.SurveyCatalogs
                                         where sc.CatalogID == surveyCatalogID
                                         select sc).Single();
                    try
                    {
                        surveyCatalog.CatalogName = surveyCatalogName;
                        projectEntity.SaveChanges();
                        result = 10;
                    }
                    catch
                    {
                        result = 20;
                    }
                }
                catch
                {
                    result = 30;
                }
            }
            else
            {
                result = 40;
            }
            return result;
        }

        //Get survey catalog by survey catalog id funcation
        public string getSurveyCatalogName(int _idSurveyCatalog)
        {
            int idSurveyCatalog = _idSurveyCatalog;
            string surveyCatalogName = "";
            try
            {
                var surveyCatalog = (from sc in projectEntity.SurveyCatalogs
                                     where sc.CatalogID == idSurveyCatalog
                                     select sc).Single();
                surveyCatalogName = surveyCatalog.CatalogName;
            }
            catch
            {
                surveyCatalogName = "";
            }
            return surveyCatalogName;
        }

        //Delete survey catalog by survey catalog id
        public int deleteSurveyCatalogById(int _idSurveyCatalog)
        {
            int idSurveyCatalog = _idSurveyCatalog;
            int result = 0;
            try
            {
                var surveyCatalog = (from sc in projectEntity.SurveyCatalogs
                                     where sc.CatalogID == idSurveyCatalog
                                     select sc).Single();
                projectEntity.SurveyCatalogs.Remove(surveyCatalog);
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 20;
            }
            return result;
        }

        //Get all survey function
        public List<Survey> getAllSurvey()
        {
            List<Survey> listSurvey = new List<Survey>();
            try
            {
                listSurvey = (from s in projectEntity.Surveys
                              select s).ToList();
            }
            catch
            {
                listSurvey = null;
            }
            return listSurvey;
        }

        //Create new survey function
        public int createNewSurvey(Survey _sv)
        {
            int result = 0;
            try
            {
                Survey sv = _sv;
                projectEntity.Surveys.Add(sv);
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 20;
            }
            return result;
        }

        //Insert questions to survey function
        public int insertQuestionToSurvey(List<Question> _listQuestion)
        {
            int result = 0;
            List<Question> listQuestion = _listQuestion;
            try
            {
                foreach (Question q in listQuestion)
                {
                    projectEntity.Questions.Add(q);
                }
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 20;
            }
            return result;
        }

        //Delete survey by survey id
        public int deleteSurveyBySurveyID(int _surveyID)
        {
            int result = 0;
            int surveyID = _surveyID;
            Survey sv = (from s in projectEntity.Surveys
                         where s.SurveyID == surveyID
                         select s).Single();
            try
            {
                List<Question> listQuestion = (from q in projectEntity.Questions
                                               where q.SurveyID == surveyID
                                               select q).ToList();
                foreach (Question q in listQuestion)
                {
                    int questionID = q.QuestionID;
                    try
                    {
                        List<Answer> listAnswer = (from a in projectEntity.Answers
                                                   where a.QuestionID == questionID
                                                   select a).ToList();
                        foreach (Answer a in listAnswer)
                        {
                            projectEntity.Answers.Remove(a);
                        }
                    }
                    catch
                    {
                        result = 90;
                    }
                    projectEntity.Questions.Remove(q);
                }
                projectEntity.Surveys.Remove(sv);
                result = 10;
            }
            catch
            {
                result = 100;
            }
            if (result == 10)
            {
                projectEntity.SaveChanges();
            }
            return result;
        }

        //Get survey by id
        public Survey getSurveyByID(int _surveyID)
        {
            Survey sv;
            int surveyID = _surveyID;
            try
            {
                sv = (from s in projectEntity.Surveys
                      where s.SurveyID == surveyID
                      select s).SingleOrDefault();
            }
            catch
            {
                sv = null;
            }
            return sv;
        }

        //Update survey info
        public int updateSurveyInfo(int _surveyID, string _surveyName, string _surveyDesc, string _surveyImage, DateTime _surveyStartDate, DateTime _surveyDeadLine, int _catalogID, int _groupID)
        {
            int result = 0;
            try
            {
                Survey s = (from su in projectEntity.Surveys
                            where su.SurveyID == _surveyID
                            select su).SingleOrDefault();
                s.SurveyName = _surveyName;
                s.SurveyDesc = _surveyDesc;
                s.SurveyImage = _surveyImage;
                s.SurveyStartDate = _surveyStartDate;
                s.SurveyDateLine = _surveyDeadLine;
                s.CatalogID = _catalogID;
                s.SurveyPlayer = _groupID;
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 100;
            }
            return result;
        }

        //Get all question of survey by survey id function
        public List<Question> getAllQuestionBySurveyID(int _surveyID)
        {
            List<Question> listQuestion;
            try
            {
                listQuestion = (from q in projectEntity.Questions
                                where q.SurveyID == _surveyID
                                select q).ToList();
            }
            catch
            {
                listQuestion = null;
            }
            return listQuestion;
        }

        //Delete question by question id function
        public int deleteQuestionByID(int _questionID)
        {
            int result = 0;
            int questionID = _questionID;
            try
            {
                Question question = (from q in projectEntity.Questions
                                     where q.QuestionID == questionID
                                     select q).First();
                projectEntity.Questions.Remove(question);
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 100;
            }
            return result;
        }

        //Get Question by question id
        public Question getQuestionByID(int _questionID)
        {
            int questionID = _questionID;
            Question question;
            try
            {
                question = (from q in projectEntity.Questions
                            where q.QuestionID == questionID
                            select q).First();
            }
            catch
            {
                question = null;
            } return question;
        }

        //Update question by question id
        public int updateQuestionByQuestion(Question _question)
        {
            int result = 0;
            Question Ques = _question;
            try
            {
                Question question = (from q in projectEntity.Questions
                                     where q.QuestionID == Ques.QuestionID
                                     select q).First();
                question.Answers = Ques.Answers;
                question.QuestionTypeID = Ques.QuestionTypeID;
                question.QuestionDetail = Ques.QuestionDetail;
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 100;
            }
            return result;
        }

        //Get answers by question id
        public String getAnswersListFromQuestionID(int questionID)
        {
            var result = (from question in projectEntity.Questions
                          where question.QuestionID == questionID
                          select question).Single();
            return result.Answers;
        }

        //Get answers comment by question id

        public List<Answer> getAnswersCommentByQuestionID(int questionID)
        {
            List<Answer> listAnswers = new List<Answer>();
            try
            {
                listAnswers = (from answer in projectEntity.Answers
                               where answer.QuestionID == questionID
                               select answer).ToList();
            }
            catch
            {
                listAnswers = null;
            }
            return listAnswers;
        }

        //Get result answers by question ID
        public List<Result> getResultListFromQuestionID(int questionID)
        {
            float total = 0;
            List<Result> arrResults = new List<Result>();
            String answers = this.getAnswersListFromQuestionID(questionID);
            //answers = answers.Substring(6, answers.Length - 6);
            //answers = answers.Substring(0, answers.Length - 6);
            string[] words = answers.Split('#');
            foreach (string word in words)
            {
                Result result = new Result();
                result.title = word;
                arrResults.Add(result);
            }


            List<String> arrStrings = new List<string>();
            var arrAnswers = (from answer in projectEntity.Answers
                              where answer.QuestionID == questionID
                              select answer).ToList();
            foreach (Answer answer in arrAnswers)
            {
                string[] word = answer.Answers.Split('#');
                foreach (String w in word)
                {
                    total += 1;
                    foreach (Result r in arrResults)
                    {
                        if (r.title.Equals(w))
                        {
                            r.count += 1;
                            break;
                        }
                    }
                }
            }

            foreach (Result r in arrResults)
            {
                r.percent = (r.count / total * 100).ToString().Replace(",", ".");
            }
            return arrResults;
        }

        //Get list answer in question function
        public string[] getListAnswer(int questionID)
        {
            var listAnswer = (from a in projectEntity.Questions
                              where a.QuestionID == questionID
                              select a).Single();
            string[] word = listAnswer.Answers.Split('#');
            return word;
        }

        //Update status survey function
        public int updateStatusSurvey(int idSurvey, int status)
        {
            try
            {
                Survey sv = (from s in projectEntity.Surveys
                             where s.SurveyID == idSurvey
                             select s).Single();
                sv.SurveyStatus = status;
                projectEntity.SaveChanges();
                return 10;
            }
            catch
            {
                return 100;
            }
        }

        //Update winer
        public int updateWinner(int surveyID, int mark, string winer)
        {
            try
            {
                Winer w = new Winer();
                w.Mark = mark;
                w.Name = winer;
                w.SurveyID = surveyID;
                projectEntity.Winers.Add(w);
                projectEntity.SaveChanges();
                return 10;
            }
            catch
            {
                return 100;
            }
        }
    }
}